package com.appkubes.mall.restful.application.goods.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.Response;
import com.appkubes.mall.core.entity.MallGoodsExtend;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.goods.goods.entity.dto.MallGoodDetailDto;
import com.appkubes.mall.restful.application.goods.goods.entity.pojo.ColorAttribute;
import com.appkubes.mall.restful.application.goods.goods.entity.vo.GoodsesVO;
import com.appkubes.mall.restful.application.goods.goods.entity.vo.MallGoodVo;
import com.appkubes.mall.restful.application.goods.goods.service.GoodsExtendService;
import com.appkubes.mall.restful.application.goods.goods.service.GoodsService;
import com.appkubes.mall.restful.application.goods.goods.service.impl.GoodsServiceImpl;
import com.appkubes.mall.restful.application.goods.goods.service.impl.GoodsStageServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api("获取商品模块")
@RestController
@RequestMapping(value = "/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    GoodsService goodsSercive;

    @Autowired
    GoodsStageServiceImpl stageService;

    @Resource
    GoodsExtendService goodsExtendService;


    @ApiOperation("获取商品详情")
    @PostMapping("/getGoods")
    public Object getGoods(@RequestBody @Valid MallGoodVo mallGoodVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.error("参数检查失败");
        }
        MallGoodDetailDto mallGoodDetailDto = goodsSercive.getRentGoods(mallGoodVo.getGoodsKey());
        MallGoodsExtend extend = goodsExtendService.get(mallGoodVo.getGoodsKey());
        if (extend != null && !StringUtils.isEmpty(extend.getColorAttributes())) {
            mallGoodDetailDto.setColors(JSONObject.parseArray(extend.getColorAttributes(), ColorAttribute.class));
        }

        mallGoodDetailDto.setUsers(goodsService.getGoodsUserModel(mallGoodVo.getGoodsKey()));
        mallGoodDetailDto.setBuyCount(goodsService.getGoodsOrderCount(mallGoodVo.getGoodsKey()));

        return imgResponse(mallGoodDetailDto);
    }

    @ApiOperation("获取商品列表")
    @PostMapping("/getGoodses")
    public Response getGoodses(@RequestBody  @Valid GoodsesVO goodsesVO) {
        IPage goodses = goodsService.getGoodses(goodsesVO);
        return imgResponse(goodses);
    }

//    @ApiOperation("获取分类商品")
//    @PostMapping("/getGoodses")
//    public Object getGoodses(@RequestParam(defaultValue = "") String catKey,
//                             @RequestParam(defaultValue = "1") Integer page,
//                             @RequestParam(defaultValue = "10") Integer limit,
//                             @RequestParam(defaultValue = "") String leaderKey,
//                             @RequestParam(defaultValue = "0.0") Double longitude,
//                             @RequestParam(defaultValue = "0.0") Double latitude) {
//
//        //如果没有传递团长id，就需要传递经纬度，获取最近团长id
//        if (StringUtils.isEmpty(leaderKey)) {
//            leaderKey = "";
//        }
//        String queryStr = " 1=1 ";
//
//        return Response.success(goodsService.getList(queryStr, " store_key,`sort` ", page, limit));
//    }


    @ApiOperation("获取商品分期配置")
    @PostMapping("/get")
    public Response get(@RequestParam(defaultValue = "") String goodKey) {
        return Response.success(stageService.getById(goodKey));
    }


}
